﻿using UnityEngine;
using UnityEngine.EventSystems;
using Yoozoo.Mars.Got;
using Yoozoo.Gameplay.RTS;
using Yoozoo.Gameplay.RTS.Proto;
using System;
using DestroyIt;
using NodeCanvas.Tasks.Actions;
using System.Net.Security;
using UnityEngine.Serialization;

public class MarchMono : MonoBehaviour 
{
    private SphereCollider collider;
    [FormerlySerializedAs("MarchObjType")] public EClientMarchType eClientMarchType;
    public long marchId;
    public TargetCoordinateType TargetCoordinateType; 
    private bool colliderEnable = false;

    private CapsuleCollider collider_train; // 列车胶囊碰撞体
    #region 距离声音发射器需要
    public GameObject marchAudioEmitter;
    private static Quaternion fixedAudioEmitter = Quaternion.Euler(0,235,0);
    #endregion

    private void Awake()
    {
        collider = this.AddComponent<SphereCollider>();
        collider.center = Vector3.zero;
        collider.radius = BattleUtils.ArmyScale;
        gameObject.layer = LayerMask.NameToLayer("RaycastCollider");
        colliderEnable = collider.enabled;

        marchAudioEmitter = new GameObject("AudioEmitter");
        marchAudioEmitter.transform.parent = transform;
        marchAudioEmitter.transform.localPosition = Vector3.zero;
        marchAudioEmitter.transform.localRotation = fixedAudioEmitter;
    }

    public void SetTouchable(bool active)
    {
        if (colliderEnable != active)
        {
            if (collider_train) // 当存在列车的胶囊碰撞体的时候不设置默认碰撞体
            {
                collider_train.enabled = active;
            }
            else
            {
                collider.enabled = active;
            }
            colliderEnable = active;
        }
    }

    public void SetColliderTrain(CapsuleCollider collider)
    {
        collider_train = collider;
        collider_train.enabled = colliderEnable;
        collider_train.name = gameObject.name;
        collider.enabled = false;
    }

    public void SetColliderTrainName()
    {
        if (collider_train)
        {
            collider_train.name = gameObject.name;
            collider_train.enabled = colliderEnable;
            collider.enabled = false;
        }
    }

    public void Init(EClientMarchType eClientMarchType, long id,TargetCoordinateType targetCoordinateType,bool colliderEnable = true)
    {
        this.eClientMarchType = eClientMarchType;
        marchId = id;
        TargetCoordinateType = targetCoordinateType;
        // 下面SetColliderRadius方法看外部有地方调用了，但是不知道为啥，碰撞体的半径没设置成功，也有可能什么地方又设置回去了，这里再走一次
        if (targetCoordinateType == TargetCoordinateType.ALLIANCE_PRACTICE_BOSS_32)
        {
            collider.radius = 0.8f;
        }
        SetTouchable(colliderEnable);
        this.colliderEnable = colliderEnable;
    }

    public void SetTeam(int team)
    {
        if (team == 0)
        {
            float ratio = 1.25f;
            collider.radius = BattleUtils.ArmyScale  * ratio;
        }
        else
        {
            collider.radius = BattleUtils.ArmyScale ;
        }
    }
    
    public void SetColliderRadius(int team, TargetCoordinateType type)
    {
        if (type == TargetCoordinateType.ALLIANCE_PRACTICE_BOSS_32)
        {
            collider.radius = 0.8f;
        }
        else if (type == TargetCoordinateType.DONALD_BOSS_38)
        {
            collider.radius = 0.8f;
        }
        if (type == TargetCoordinateType.WORLD_BOSS_44)
        {
            collider.radius = 1.2f;
        }
        else if(type == TargetCoordinateType.MONSTER_4)
        {
            collider.radius = 0.5f;
        }
        else if(type == TargetCoordinateType.TRAIN_51)
        {
        }
        else
        {
            if (team == 0)
            {
                float ratio = 1.25f;
                collider.radius = BattleUtils.ArmyScale * ratio;
            }
            else
            {
                collider.radius = BattleUtils.ArmyScale;
            }
        }
    }
    public void SetColliderRadius(float radius)
    {
        collider.radius = radius;
    }
    public void Dispose()
    {
        collider.enabled = false;
        colliderEnable = false;
        if (collider_train)
        {
            collider_train.enabled = false;
            collider_train.gameObject.name = "colliderTrain";
            collider_train = null;
        }
    }

    internal void SetLayerTop()
    {
        var childCount = transform.parent.childCount;
        transform.SetSiblingIndex(childCount - 1);
    }

    private void OnEnable()
    {
       
    }

    private void OnDisable()
    {
     
    }
}
